<?php

class DisplayController extends Controller
{
	
	public $layout='//layouts/main';
	
	
	
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Product',array(
		    'id'=>'product',
		    'sort'=>array(
		        'attributes'=>array(
		             'id', 'name', 'fullname',
		        ),
		    ),
		    'pagination'=>array(
		        'pageSize'=>15,
		    ),
		));
		$this->pageTitle = Lang::getResource("product.title");
		$this->render('index',array(
				'dataProvider'=>$dataProvider,
		));
	}
	
	public function actionSearch($s){
		$criteria = new CDbCriteria;
		$criteria->select='*';  // only select the 'title' column
		$criteria->condition='name like :name or fullname like :name';
		$criteria->params=array(':name'=>'%'.$s.'%');
		$products=Product::model()->findAll($criteria);
		$dataProvider = new CArrayDataProvider($products, array(
		    'id'=>'product',
		    'sort'=>array(
		        'attributes'=>array(
		             'id', 'name', 'fullname',
		        ),
		    ),
		    'pagination'=>array(
		        'pageSize'=>15,
		    ),
		));
		$this->render('search',array(
				'dataProvider'=>$dataProvider,
		));
	}
	
	public function actionCate($id){
		$cate = Category::model()->findByPk($id);
		$this->pageTitle = $cate->name;
		
		$criteria = new CDbCriteria;
		$criteria->select='*';  
		$criteria->condition='cateId = :cateId';
		$criteria->params=array(':cateId'=> $id);
		$products=Product::model()->findAll($criteria);
		
		$dataProvider = new CArrayDataProvider($products, array(
				'id'=>'product',
				'sort'=>array(
						'attributes'=>array(
								'id', 'name', 'fullname',
						),
				),
				'pagination'=>array(
						'pageSize'=>10,
				),
		));
		
		$this->render('search',array(
				'dataProvider'=>$dataProvider,
		));
	}
	
	public function actionDetail($id){
		$criteria = new CDbCriteria;
		$criteria->select='*';
		$criteria->condition='id = :id';
		$criteria->params=array(':id'=> $id);
		$product=Product::model()->find($criteria);
		$this->pageTitle = $product->name;
		$this->render('detail',array(
				'product'=>$product,
		));
	}
	
	public function actionPosts(){
		$criteria = new CDbCriteria;
		$criteria->limit = 10;
		$criteria->order = 'position DESC, createdDate DESC';		
		$posts=Post::model()->findAll($criteria);
		
		$dataProvider = new CArrayDataProvider($posts, array(
				'id'=>'id',
				'sort'=>array(
						'attributes'=>array(
								'createdDate', 'title'
						),
				),
				'pagination'=>array(
						'pageSize'=>5,
				),
		));
		$this->pageTitle = Lang::getResource("news.title");
		$this->render('posts',array(
				'dataProvider'=>$dataProvider,
		));
	}
	
	public function actionPost($id){
		$posts=Post::model()->findAll();
		
		$dataProvider = new CArrayDataProvider($posts, array(
				'id'=>'id',
				'sort'=>array(
						'attributes'=>array(
								'createdDate', 'title'
						),
				),
				'pagination'=>array(
						'pageSize'=>10,
				),
		));
		
		$criteria = new CDbCriteria;
		$criteria->select='*';
		$criteria->condition='id = :id';
		$criteria->params=array(':id'=> $id);
		$post=Post::model()->find($criteria);
		
		$criteria = new CDbCriteria;
		$criteria->select='*';
		$criteria->condition='postId = :id';
		$criteria->params=array(':id'=> $id);
		$comments = Comment::model()->findAll($criteria);
		
		$this->pageTitle = $post->title;
		
		$this->render('post',array(
				'post'=>$post,
				'dataProvider'=>$dataProvider,
				'model'=>new Comment(),
				'comments'=>$comments,
		));
	}
	
	public function actionPostComment(){
		$model=new Comment;
		if(isset($_POST['Comment']))
		{
			$model->attributes=$_POST['Comment'];
			//vietnam time
			$xml = simplexml_load_file('http://www.earthtools.org/timezone/16/106');
			$strLocal = $xml->localtime;
			$date = date_parse_from_format('d M Y H:i:s',$strLocal);
			$model->createdDate = $date['year'].' -'.$date['month'].' -'.$date['day'].' '.
						$date['hour'].':'.$date['minute'].':'.$date['second'] ;
			if($model->save())
				$this->redirect(array('post','id'=>$model->postId));
		}
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	*/
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
				// captcha action renders the CAPTCHA image displayed on the contact page
				'captcha'=>array(
						'class'=>'CCaptchaAction',
						'backColor'=>0xFFFFFF,
				),
				
		);
	}
}